package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import module.IndexModule;

public class GetIndexMsg extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/json");
		response.setCharacterEncoding("UTF-8");
		Gson gson=new Gson();
		IndexModule indexModule=new IndexModule();
		PrintWriter out = response.getWriter();
		String action=request.getParameter("action");
		if(action.equals("getIndexMader")){
			try {
				ArrayList list=indexModule.getMaderMsg();
				String indexMaderjson=gson.toJson(list);
				out.print(indexMaderjson);
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if(action.equals("getIndexItem1")){
			try {
				ArrayList list=indexModule.getItem1();
				String indexItem1json=gson.toJson(list);
				out.print(indexItem1json);
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if(action.equals("getIndexItem2")){
			try {
				ArrayList list=indexModule.getItem2();
				String indexItem1json=gson.toJson(list);
				out.print(indexItem1json);
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		out.flush();
		out.close();
	}

}
